import controllers from './controllers'
const web = controllers.web

class MetaModel {
  constructor() {}

  static get _name() {
    return this._model
  }

  static get env() {
    return this._env
  }

  static async execute_kw(method, args = [], kwargs = {}) {
    const kwargs2 = { ...kwargs }

    if (!Object.keys(kwargs).includes('context'))
      kwargs2.context = this.env.context

    const payload = { model: this._name, method, args, kwargs: kwargs2 }
    return web.dataset.call_kw(payload)
  }

  static async execute(method, ...args) {
    return this.execute_kw(method, args, {})
  }
}

MetaModel._env = undefined
MetaModel._model = undefined

export class Model extends MetaModel {
  constructor(payload = {}) {
    super(payload)
  }

  static async search_read(kwargs = {}) {
    return this.execute_kw('search_read', [], kwargs)
  }
}
